package org.example.ims.Entity.Base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import jakarta.persistence.*;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonBackReference;
import org.example.ims.Utils.ProductUnit;
import org.example.ims.Entity.WareHouseProduct.WarehouseProduct;

/*商品信息*/
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer productId;

    private String productName;

    private Double productPrice;

    private Integer productStock=0;

    private String productStatus;

    @Enumerated(EnumType.STRING)
    private ProductUnit unit;  // 使用枚举类型

    @ManyToOne
    @JoinColumn(name = "category_id")
    @JsonBackReference
    private Category category;

    @OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
    @JsonIgnore
    private List<WarehouseProduct> warehouseProducts;

    @Transient
    @JsonProperty("categoryId") //暴露给 JSON 序列化和反序列化
    public Integer getCategoryId() {
        return this.category != null ? this.category.getCategoryId() : null;
    }

    @Transient
    @JsonProperty("categoryId")
    public void setCategoryId(Integer categoryId) {
        if (categoryId != null) {
            this.category = new Category();
            this.category.setCategoryId(categoryId);
        }
    }
}